package com.song.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.song.constant.MessageConstant;
import com.song.constant.StatusConstant;
import com.song.dto.DishDTO;;
import com.song.dto.DishPageQueryDTO;
import com.song.entity.Dish;
import com.song.entity.DishFlavor;
import com.song.exception.DeletionNotAllowedException;
import com.song.mapper.DishMapper;
import com.song.mapper.FlavorMapper;
import com.song.result.PageResult;
import com.song.service.DishService;
import com.song.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private FlavorMapper flavorMapper;
    //新增菜品和对应的口味
    @Transactional
    @Override
    public Integer addDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.addDish(dish);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.forEach(flavor -> {
            flavor.setDishId(dish.getId());
        });
        return flavorMapper.addFlavor(flavors);
    }
    @Transactional
    @Override
    public PageResult<DishVO> getDishPage(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        List<DishVO> dishVOList = dishMapper.getDishPage(dishPageQueryDTO);
        //查询菜品对应的口味列表
        List<DishFlavor> dishFlavorList = flavorMapper.getDishFlavorList(dishPageQueryDTO);
        //根据菜品列表和口味列表中的菜品id进行关联
        dishVOList.forEach(dishVO -> {
            dishVO.setFlavors(dishFlavorList.stream().filter(dishFlavor -> dishFlavor.getDishId().equals(dishVO.getId())).toList());
        });
        Page<DishVO> page = (Page<DishVO>) dishVOList;
        return new PageResult<>(page.getTotal(),dishVOList);
    }
    @Transactional
    @Override
    public Integer deleteDish(List<Long> ids) {
      //根据菜品ids查询在售的菜品数量
       if(!dishMapper.getSaleDishByIds(ids).isEmpty()){
           throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
       }
       //根据菜品ids查询,菜品关联的套餐数量
        if(dishMapper.getDishSetmealCountByIds(ids)>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品表中的菜品数据
        dishMapper.deleteDish(ids);
        //删除菜品口味表数据
        return flavorMapper.deleteDishFlavor(ids);
    }
    @Transactional
    @Override
    public Integer updateDish(DishDTO dishDTO) {
        //1.更新菜品表数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.updateDish(dish);
        //2.更新口味表数据
        //2.1.删除原有的口味数据
        flavorMapper.deleteDishFlavor(Collections.singletonList(dishDTO.getId()));
        //2.2.添加新的口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors == null) return 1;
        return flavorMapper.addFlavor(flavors);
    }

    @Override
    public DishVO getDishByID(Long id) {
        //1.查询菜品数据
        DishVO dishVO = dishMapper.getDishByID(id);
        //2.查询菜品口味数据
        dishVO.setFlavors(flavorMapper.getFlavorListByDishId(id));
        return dishVO;
    }

    @Override
    public List<Dish> getDishListByCategoryId(Long categoryId) {
      return dishMapper.getDishListByCategoryId(categoryId);
    }

    @Override
    public Integer updateDishStatus(Integer status, Long id) {
      return dishMapper.updateDishStatus(status,id);
    }

    @Override
    public List<DishVO> getDishAndFlavorList(Long categoryId) {
        DishPageQueryDTO dishVO = new DishPageQueryDTO();
        dishVO.setStatus(StatusConstant.ENABLE);
        dishVO.setCategoryId(categoryId.toString());
       List<DishVO> dishVOList = dishMapper.getDishPage(dishVO);
       // 根据菜品id获取口味数据
        dishVOList.forEach(dish -> {
            dish.setFlavors(flavorMapper.getFlavorListByDishId(dish.getId()));
        });
        return dishVOList;

    }
}
